package com.trackerapp.network;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;

public class Httpconnection1 {

	public static void sendLocationTask(String latitude, String longitude, int accuracy) {
		
		AsyncTask<String, Void, Void> sendLocationTask = new AsyncTask<String, Void, Void>(){

			@Override
			protected Void doInBackground(String...params) {
				sendLocation(params[0],params[1],params[2]);
				return null;
			}
			
			protected void onPostExecute(String result) {
		        
		    }			
		};
		
		sendLocationTask.execute(latitude,longitude,Integer.toString(accuracy));
	}
	
	private static void sendLocation(String latitude, String longitude, String accuracy) {
	    // Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost("http://ec2-54-242-153-20.compute-1.amazonaws.com/update_location");

	    try {
	        // Add your data
	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
	        nameValuePairs.add(new BasicNameValuePair("latitude", latitude ));
	        nameValuePairs.add(new BasicNameValuePair("longitude",longitude ));
	        nameValuePairs.add(new BasicNameValuePair("accuracy", accuracy));
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	        // Execute HTTP Post Request
	        HttpResponse response = httpclient.execute(httppost);
	        
	    } catch (ClientProtocolException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    }
	} 
	
	public static void registrationTask(String latitude, String longitude, int accuracy) {
		
		AsyncTask<String, Void, Void> registrationTask = new AsyncTask<String, Void, Void>(){

			@Override
			protected Void doInBackground(String...params) {
				register(params[0],params[1]);
				return null;
			}
			
			protected void onPostExecute(String result) {
		        
		    }
		};
		
		
		registrationTask.execute(latitude,longitude,Integer.toString(accuracy));
	}

	private static void register(String phonenumber, String accesscode) {
	    // Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost("http://ec2-54-242-153-20.compute-1.amazonaws.com/verify_accesscode");

	    try {
	        // Add your data
	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
	        nameValuePairs.add(new BasicNameValuePair("phonenumber", phonenumber ));
	        nameValuePairs.add(new BasicNameValuePair("accesscode",accesscode ));
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	        // Execute HTTP Post Request
	        HttpResponse response = httpclient.execute(httppost);
	        
	    } catch (ClientProtocolException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    }
	} 
}
